﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FormControllers;
using AgencyEntityLib.BaseEntity;
using Ecopsy.Tools;
using SqlLibrary;
using Ecopsy.DataTier;

public partial class Ctl_ctlContactFaces : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Init();
    }

    private void Init()
    {

        //привязать событие открыть закрыть lvContactFace
        if (lvContactFaces.EditItem != null)
        {

            Ctl_ctlContactFace r1 = (Ctl_ctlContactFace)lvContactFaces.EditItem.FindControl("ctlContactFace2");
            r1.OnButtonClick = OnContactFaceClick;
            r1.OnMyDataChanged = OnContactFaceClick;
        }
    }

    protected  string GetCompanyName(ContactFace Face)
    {

        return Face.GetClient().Name;
    }


    protected bool OnContactFaceClick()
    {
        lvContactFaces.EditIndex = -1;
        DataBind ();
        return true;
    }

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);

        ObjectDS_PF ODS = new ObjectDS_PF(GetList);
        lvContactFaces.DataSource = ODS.ImAsObjectDataSource;
        lvContactFaces.DataBind();
    }

    int oldRowCount = 0;
    ContactFace[] rs = null;
    protected ContactFace[] GetList(int startRowIndex, int maximumRows, out int RowCount)
    {
        if (rs != null) { RowCount = oldRowCount; return rs; }

        wsFace ws = (wsFace)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];
        ListWhere w = (ListWhere)Where;

        w.ListLoadStatment.StartRowIndex = startRowIndex;
        w.ListLoadStatment.EndRowIndex = maximumRows;
        ContactFaceList l = ws.GetContactFaceList((ContactFaceList.sqlLoadStatment)w.ListLoadStatment);
        oldRowCount = RowCount = l.OutputRwCnt;
        
        ////////заряжаем comparer функцией сортировки 
        rs = l.ToArray<ContactFace>();
        Where.SortCondition.fnOrderBy = SortFunctions.fOrderBy;        
        Array.Sort(rs, Where.SortCondition);
        return rs;
    }



  

    /// <summary>
    /// Лексема
    /// </summary>
    public string Lexeme { get { return Where.ListLoadStatment.Lexeme; } set { Where.ListLoadStatment.Lexeme = value; } }

    /// <summary>
    /// Набор пар = параметр значение, сформировать запрос к 
    /// Коллекция - vPropertyOfList.ParamListItem
    /// </summary>       
    public EntityList ParamValueList
        { get { return Where.ListLoadStatment.ParamValueList; }}


    
    public ListWhere Where
    {
        get
        {
            string key = "Where" + KeyPref;
            if (!CacheTools.IsSessionCacheObject(key))
            {
                ListWhere r = new ListWhere();                
                r.ListLoadStatment = new ContactFaceList.sqlLoadStatment();

                r.SortCondition.SortDirection = SortDirection.Ascending;
                r.SortCondition.SortField = "FirstName";
                CacheTools.SetSessionCacheObject(key, r);
            }
            return (ListWhere)CacheTools.GetSessionCacheObect(key);
        }

        set
        {
            string key = "Where" + KeyPref;
            CacheTools.ResetSessionCache(key);
            CacheTools.SetSessionCacheObject(key, value);
        }

    }

    /// <summary>
    /// префикс - для хранения в кэше 
    /// </summary>
    public string KeyPref { get { return hfPref.Value + HttpContext.Current.User.Identity.Name; } set { hfPref.Value = value; } }

    protected void BntRemoveWorker_Click(object sender, EventArgs e)
    {

        ImageButton btn =(ImageButton) sender ;
        wsFace ws = (wsFace)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];
        ContactFace f = ws.GetContactFace (btn.CommandArgument);
        f.DestroyMySelf();
        ws.SaveFace(f);
        DataBind();
    }

    protected void ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Edit":
                {

                    ListViewDataItem d = (ListViewDataItem)e.Item;
                    lvContactFaces.EditIndex = d.DataItemIndex;
                    break;
                }
            case "Update":
                {
                    lvContactFaces.EditIndex = -1;
                    break;
                }

        }
            this.DataBind();
    }

    protected void ItemEditing(object sender, ListViewEditEventArgs e)
    { 
    }
        
    protected void ItemDataBound(object sender, ListViewItemEventArgs e)
    {
      

    }
    
    protected void ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
    }

    public int PageSize
    {
        get { return Convert.ToInt16(hfPageSize.Value); }
        set { hfPageSize.Value = value.ToString(); }
    }
   
    public bool PagerVisible 
    {
        get { return true; }
    }

    protected void lvContactFaces_PagePropertiesChanged(object sender, EventArgs e)
    {
        DataBind();
    }

    protected string GetHeadCss(string HearTd)
    {
        if (Where.SortCondition.SortField != HearTd) return string.Empty;
        return "sort" + Where.SortCondition.SortDirection.ToString();
    }

    protected void lvSort(object sender, EventArgs e)
    {
        lvContactFaces.EditIndex = -1;
        string s = ((LinkButton)sender).CommandArgument;
        Where.SortCondition.SortDirection = (Where.SortCondition.SortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending); ;
        Where.SortCondition.SortField = s;
        DataBind();
    }
    private bool _ShowHead = false;
    public bool ShowHead { get { return _ShowHead; } set { _ShowHead = value; } }
}
